package com.yyfax.pay.order.controller.api;
/*
 * @Copyright: 2017 www.yyfax.com Inc. All rights reserved.
 */

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.common.Log;
import com.yyfax.pay.common.enums.PaymentEnum;
import com.yyfax.pay.config.data.service.MerchantConfigService;
import com.yyfax.pay.config.model.po.MerchantConfigPO;
import com.yyfax.pay.order.service.AdapterService;
import com.yyfax.pay.order.service.PayoutService;
import com.yyfax.pay.third.model.to.yyfax.PayoutRefundDataBinder;

/**
 * 代付异步通知
 * 
 * @author buyi
 * @date 2017-04-12 20:23:19
 * @since v1.0.0
 */
@RestController
public class PayoutResultNotifyController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private PayoutService payoutService;

	@Autowired
	private AdapterService adapterService;

	@Resource
	private MerchantConfigService merchantConfigService;

	/**
	 * 代付退票异步通知<br>
	 * 版本2.1.5只包含了通联代付退票
	 * 
	 * @author buyi
	 * @date 2018-05-23 14:58:02
	 * @since v2.1.5
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/result/payout/refund/notify/{paymentCode}/{merchantCode}")
	public String refundNotify(@PathVariable("merchantCode") String merchantCode, @PathVariable("paymentCode") String paymentCode,
			HttpServletRequest request) throws Exception {
		final String op = "PayoutResultNotifyController.refundNotify";

		PaymentEnum paymentEnum = PaymentEnum.fromValue(paymentCode);
		if (paymentEnum == null) {
			logger.warn(Log.op(op).msg("代付 | 退票通知 | 请求支付通道参数非法").kv("paymentCode", paymentCode).toString());
			return null;
		}

		// 判断商户号合法性
		MerchantConfigPO merchantConfig = merchantConfigService.queryByMerchantCode(merchantCode);
		if (merchantConfig == null) {
			// 非法商户请求，直接返回
			logger.warn(Log.op(op).msg("非法请求，商户号不合法").kv("merchantCode", merchantCode).toString());
			return "";
		}

		// 解析退票通知
		PayoutRefundDataBinder binder = adapterService.adapterPayService(paymentEnum).parsePayoutRefundNotifyComposite(merchantCode, request);
		if (binder.isError()) {
			return "";
		}

		// 更新代付订单退票结果
		payoutService.processRefundNotify(binder);

		return binder.getRspMsg();
	}

	// /**
	// * 代付异步通知入口
	// *
	// * @author buyi
	// * @date 2017-04-18 11:39:12
	// * @since v1.0.0
	// * @param payment
	// * @param request
	// * @return
	// * @throws Throwable
	// */
	// @RequestMapping(value = "/api/result/payout/notify/{paymentCode}")
	// public String orderNotify(@PathVariable("paymentCode") String paymentCode, HttpServletRequest request) throws Throwable {
	// PaymentEnum paymentEnum = PaymentEnum.fromValue(paymentCode);
	// if (paymentEnum == null) {
	// return null;
	// }
	//
	// ThirdPayService thirdPayService = adapterService.adapterPayService(paymentEnum);
	// if (thirdPayService == null) {
	// logger.info("没有适配到支付通道：{}的service", paymentCode);
	// return null;
	// }
	//
	// // 解析异步回调请求
	// OrderNotifyContent notifyContent = thirdPayService.transformPayoutOrderNotify(request);
	//
	// if (notifyContent == null || PayRspCodeEnum.PAY_CODE_CHECK_SIGN_FAIL.isValue(notifyContent.getResultCode())) { // 如果验签失败
	// logger.info("支付通道：{}异步响应异常/签名失败", paymentEnum.getValue());
	// return null;
	// }
	//
	// // 处理异步响应
	// String rsp = payoutService.processResultNotify(paymentCode, notifyContent);
	//
	// return rsp;
	// }

}
